﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using StudySL4BizApp.Web;

namespace StudySL4BizApp.Views
{
    public partial class NewOrderDetail : ChildWindow
    {
        public Web.Order_Detail OrderDetail;

        public StudySL4BizApp.Web.Services.NorthWindContext DomainContext { get; set; }
        public StudySL4BizApp.Web.Order SelectedOrder { get; set; }

        public NewOrderDetail()
        {
            InitializeComponent();

            OrderDetail = new Order_Detail();
            dataForm1.CurrentItem = OrderDetail;
        }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            dataForm1.CommitEdit();
            if (dataForm1.IsItemValid)
            {
                OrderDetail.ValidationErrors.Clear();

                ValidationContext vc = new ValidationContext(OrderDetail, null, null);
                ICollection<ValidationResult> validationResults = new List<ValidationResult>();
                if (Validator.TryValidateObject(OrderDetail, vc, validationResults, true) == false)
                {
                    OrderDetail.ValidationErrors.Clear();
                    foreach (ValidationResult error in validationResults)
                    {
                        OrderDetail.ValidationErrors.Add(error);
                    }
                    return;
                }

                OrderContainsOrderDetail();
                if (OrderDetail.HasValidationErrors)
                {
                    return;
                }

                OrderDetail.Validate();
                if (OrderDetail.HasValidationErrors)
                {
                    return;
                }

                this.DialogResult = true;
            }
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }

        private ValidationResult _detailDuplicateError;

        private bool OrderContainsOrderDetail()
        {

            var productIdExistsInOroder = (from d in SelectedOrder.Order_Details
                                           where d.ProductID == OrderDetail.ProductID
                                           select d.ProductID).Any();
            if (productIdExistsInOroder)
            {
                _detailDuplicateError = new ValidationResult("Product Id already exists in order", new string[] { "ProductID" });

                OrderDetail.ValidationErrors.Add(_detailDuplicateError);
                return false;
            }

            if (_detailDuplicateError != null)
            {
                OrderDetail.ValidationErrors.Remove(_detailDuplicateError);
            }
            return true;
        }
    }
}

